<template>
  <d2-container>
    <el-form :model="FormSearch" status-icon label-width="100px" class="demo-ruleForm">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="条件查找" prop="good">
            <el-input
              type="text"
              v-model="FormSearch.good"
              placeholder="请输入商户名"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-input
              type="text"
              v-model="FormSearch.code"
              placeholder="请输入订单号"
              autocomplete="off"
            ></el-input>
        </el-col>
        <!-- <el-col :span="5">
          <el-date-picker
            v-model="FormSearch.timea"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="开始时间">
          </el-date-picker>
        </el-col>
        <el-col :span="5">
          <el-date-picker
            v-model="FormSearch.timeb"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="结束时间">
          </el-date-picker>
        </el-col> -->
        <el-col :span="5">
            <el-button type="primary" @click="searchClick()">查询</el-button>
            <el-button @click="resetForm()">重置</el-button>
          </el-col>
      </el-row>
    </el-form>
    <!-- 数据表格 -->
    <d2-crud
      ref="d2Crud"
      edit-title="编辑"
      add-title="添加缴费项目"
      :columns="columns"
      :data="data"
      :rowHandle="rowHandle"
      :edit-template="editTemplate"
      :add-template="addTemplate"
      :add-rules="addRules"
      :form-options="formOptions"
      :pagination="page"
      :loading="loading"
      @dialog-open="handleDialogOpen"
      @row-edit="handleRowEdit"
      @row-add="handleRowAdd"
      @dialog-cancel="handleDialogCancel"
      @custom-emit-1="createBatch"
      @custom-emit-2="delGood"
      @custom-emit-3="printOrder"
      @pagination-current-change="paginationCurrentChange"
    >
    <!-- <el-button slot="header" style="margin-bottom: 5px;" @click="addRow">添加缴费项目</el-button> -->
    </d2-crud>
    <el-dialog
      title="确认打印信息"
      :visible.sync="dialogVisible">
      <el-row>
        <el-col :span="8">
          <div id="box" v-if="orderInfo">
            <div class="top">------- 外卖订单 -------</div>
            <div class="top-diz">
              <div>地址: {{ orderInfo.saddress }}</div>
            </div>
            <div class="top1">******************************</div>
            <div class="goodsInfo" v-for="(item, index) in orderInfo.list" :key="index">
              <div>{{ item.g_name }}</div>
              <div class="money">
                <span>×{{ item.g_stock }}</span>
                <span>{{ item.money }}</span>
              </div>
            </div>
            <div class="top1">-------------- 其他--------------</div>
            <div class="goodsInfo">
              <div>总金额</div>
              <div>{{ orderInfo.sum_money }}</div>
            </div>
            <div class="goodsInfo">
              <div>优惠金额</div>
              <div>{{ orderInfo.discount_money }}</div>
            </div>
            <div class="top1">******************************</div>
            <div class="bottom">
              <div>实付款: {{ orderInfo.money }}</div>
            </div>
          </div>
        </el-col>
        <el-col :span="8"></el-col>
        <el-col :span="8"></el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="printqd">确认打印</el-button>
      </span>
    </el-dialog>
  </d2-container>
</template>

<script>
import { getYuyueOrder, addPayItem, updatePayItem, orderItemPrintConfirm, getOrderItemPrint } from "@/api/accountAdmin/index";
import util from "@/libs/util.js";

export default {
  data() {
    return {
      orderInfo: '',
      dialogVisible: false,
      loading: false,
      // 搜索数据
      FormSearch: {
        good: "",
        code: "",
        timea: "",
        timeb: "",
        value: "",
        options: [
          { value: '0', label: '未支付' },
          { value: '1', label: '已支付' },
          { value: '2', label: '已接单' },
          { value: '3', label: '配送中' },
          { value: '4', label: '全部完成' },
          { value: '5', label: '预约日期配送中' },
        ],
      },
      // 字段名称
      columns: [
        {
          title: "订单号",
          key: "code",
          align: "center"
        },
        {
          title: "商户",
          key: "sh_name",
          align: "center"
        },
        {
          title: "名称",
          key: "user_name",
          align: "center"
        },
        {
          title: "地址",
          key: "uaddress",
          align: "center"
        },
        /* {
          title: "预约开始时间",
          key: "start_time",
          align: "center"
        },
        {
          title: "预约结束时间",
          key: "end_time",
          align: "center"
        }, */
        {
          title: "支付金额",
          key: "money",
          align: "center"
        },
        {
          title: "支付状态",
          key: "state1",
          align: "center"
        },
      ],
      // 列表数据
      data: [],
      // 操作列数据
      rowHandle: {
        align: "center",
        width: 280,
        custom: [
          {
            text: "详情",
            type: "primary",
            size: "mini",
            emit: "custom-emit-1",
          },
          {
            text: "确认接单",
            type: "primary",
            size: "mini",
            emit: "custom-emit-2",
            show (index, row) {
              if (row.state == 1) {
                return true
              }
              return false
            },
          },
          /* {
            text: "打印订单",
            type: "primary",
            size: "mini",
            emit: "custom-emit-3",
            show (index, row) {
              if (row.state != 0 && row.state != 1) {
                return true
              }
              return false
            },
          }, */
        ],
      },
      addTemplate: {
        name: {
          title: "缴费名称",
          value: ""
        },
        price: {
          title: "单价",
          value: ""
        },
        charge: {
          title: "额外收费",
          value: ""
        },
        type: {
          title: "缴费类型",
          value: "",
          component: {
            name: 'el-select',
            options: [
              { value: '1', label: '物业费' },
              { value: '2', label: '其他(按单价面积计算)' },
            ],
            span: 24
          }
        },
        byIsArea: {
          title: "是否按面积收费",
          value: "",
          component: {
            name: 'el-select',
            options: [
              { value: '0', label: '否' },
              { value: '1', label: '是' },
            ],
            span: 24
          }
        },
      },
      addRules: {
        name: [ { required: true, message: '请输入缴费名称', trigger: 'blur' } ],
      },
      editTemplate: {
        name: {
          title: "缴费名称",
          value: ""
        },
        price: {
          title: "单价",
          value: ""
        },
        charge: {
          title: "额外收费",
          value: ""
        },
        by_is_area1: {
          title: "是否按面积收费",
          value: "",
          component: {
            name: 'el-select',
            options: [
              { value: '0', label: '否' },
              { value: '1', label: '是' },
            ],
            span: 24
          }
        },
      },
      formOptions: {
        labelWidth: "80px",
        labelPosition: "left",
        saveLoading: false
      },
      // 分页数据
      page: {
        total: 0,
        pageIndex: 1,
        pageSize: 10
      }
    };
  },
  created() {
    let params = {
      page: this.page.pageIndex,
      limit: this.page.pageSize
    };
    this.init(params);
  },
  methods: {
    // 初始化身体数据列表
    init(params) {
      this.loading = true
      params.oType = 1
      params.shId = util.cookies.get('uuid');
      getYuyueOrder(params)
        .then(res => {
          if (res.success) {
            res.rows.map(item => {
              if (item.state == 0) {
                item.state1 = '未支付'
              } else if (item.state == 1) {
                item.state1 = '已支付'
              } else if (item.state == 2) {
                item.state1 = '已接单'
              } else if (item.state == 3) {
                item.state1 = '配送中'
              } else if (item.state == 4) {
                item.state1 = '全部完成'
              } else if (item.state == 5) {
                item.state1 = '预约日期配送中'
              }
            })
            this.data = res.rows;
            this.page.total = res.total;
          }
          this.loading = false
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err);
        });
    },

    // 普通的新增
    addRow() {
      this.$refs.d2Crud.showDialog({
        mode: "add"
      });
    },

    // 新增成功
    handleRowAdd(row, done) {
      this.formOptions.saveLoading = true;
      let params = row;
      addPayItem(params)
        .then(res => {
          if (res.success) {
            this.$message({
              message: "添加成功",
              type: "success"
            });
          }

          // done可以传入一个对象来修改提交的某个字段
          done();
          // 保存按钮的loading状态
          this.formOptions.saveLoading = false;
          // 重新加载列表
          let params = {
            page: this.page.pageIndex,
            limit: this.page.pageSize,
          };
          this.init(params);
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 分页数改变，调用
    paginationCurrentChange(pageIndex) {
      let params = {
        limit: this.page.pageSize,
        shname: this.FormSearch.good
      };
      params.page = this.page.pageIndex = pageIndex;
      params.state = this.FormSearch.value;
      params.code = this.FormSearch.code;
      params.timea = this.FormSearch.timea;
      params.timeb = this.FormSearch.timeb;
      this.init(params);
    },

    // 条件查询搜索
    searchClick() {
      let params = {
        page: 1,
        limit: 10
      };
      params.shname = this.FormSearch.good;
      params.state = this.FormSearch.value;
      params.code = this.FormSearch.code;
      params.timea = this.FormSearch.timea;
      params.timeb = this.FormSearch.timeb;
      this.init(params);
    },

    // 重置查询
    resetForm() {
      this.FormSearch.good = "";
      let params = {
        page: 1,
        limit: 10
      };
      this.init(params);
    },

    // 点击编辑时 mode 为编辑或者添加，row为点击这一行数据
    handleDialogOpen({ mode, row }) {
    },

    // 编辑提交
    handleRowEdit({ index, row }, done) {
      // 保存按钮的loading状态
      this.formOptions.saveLoading = true;
      let params = {};
      params.id = row.id;
      params.name = row.name;
      params.price = row.price;
      params.charge = row.charge;
      if (!isNaN(row.by_is_area1)) {
        params.byIsArea = row.by_is_area1
      } else {
        params.byIsArea = row.by_is_area
      }
      console.log(row)
      updatePayItem(params)
        .then(res => {
          if (res.success) {
            this.$message({
              message: "编辑成功",
              type: "success"
            });
          }
          // done可以传入一个对象来修改提交的某个字段
          done();
          // 保存按钮的loading状态
          this.formOptions.saveLoading = false;
          // 重新加载列表
          let params = {
            page: this.page.pageIndex,
            limit: this.page.pageSize
          };
          this.init(params);
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 取消编辑
    handleDialogCancel(done) {
      /* this.$message({
        message: "取消编辑",
        type: "warning"
      }); */
      done();
    },

    // 生成批次
    createBatch({ index, row }) {
      this.$router.push({
        path: "/reportForm/orderMakeInfo",
        query: {
          code: row.code,
        }
      });
    },

    // 打印订单
    printOrder({ index, row }) {
      this.dialogVisible = true
      let params = {
        code: row.code
      }
      getOrderItemPrint(params)
        .then(res => {
          if (res.success) {
            this.orderInfo = res.rows
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 打印确定
    printqd() {
      let printBox = document.getElementById('box'); //获取需要打印的内容
      let newWin = window.open(window.document.URL); //新打开一个标签页，这样不会影响现使用的页面
      let newContent =printBox.innerHTML;
      newWin.document.body.innerHTML = newContent; //将打印的部分赋值给新打开的标签页
      newWin.print();//执行打印
      newWin.close();//关闭打印窗口
      let params = {
        page: this.page.pageIndex,
        limit: this.page.pageSize,
      };
      this.init(params);
      this.dialogVisible = false
    },

    // 删除分组
    delGood({ index, row }) {
      let params = {
        code: row.code,
      }
      this.$confirm(`此操作将确认接单, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          orderItemPrintConfirm(params)
            .then(res => {
              if (res.success) {
                this.$message({
                  message: "成功",
                  type: "success"
                });
                // 重新加载列表
                let params = {
                  page: this.page.pageIndex,
                  limit: this.page.pageSize,
                };
                this.init(params);
              }
            })
            .catch(err => {
              this.$message.error(err);
            });
        })
        .catch(err => {});
    },
  }
};
</script>
<style lang="scss" scoped>
.top {
  font-size: 18px;
  font-weight: bold;
}
.top1 {
  font-size: 14px;
  font-weight: bold;
}
.goodsInfo {
  display: flex;
  justify-content: space-between;
  .money {
    width: 50px;
    display: flex;
    justify-content: space-between;
  }
}
.bottom {
  font-weight: bold;
  display: flex;
  justify-content: flex-end;
}
.top-diz {
  font-weight: bold;
  display: flex;
  margin: 10px 0;
}
</style>
